package com.lineage.data.npc.quest2;

import com.lineage.data.executor.NpcExecutor;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1SkillMode;
import com.lineage.server.model.skill.skillmode.SkillMode;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1QuestUser;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.world.WorldQuest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.TimerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Npc_DragonB2 extends NpcExecutor {
  private static final Log _log = LogFactory.getLog(Npc_DragonB2.class);
  
  public static final Map<Integer, checkDragonTimer2> _timer = new HashMap<>();
  
  public static NpcExecutor get() {
    return new Npc_DragonB2();
  }
  
  public int type() {
    return 3;
  }
  
  public void talk(L1PcInstance pc, L1NpcInstance npc) {
    try {
        if (pc.hasSkillEffect(4009))
          pc.removeSkillEffect(4009); 
        if (pc.hasSkillEffect(4018))
          pc.removeSkillEffect(4018); 
        if (pc.hasSkillEffect(4010))
          return; 
        pc.sendPacketsX8((ServerBasePacket)new S_SkillSound(pc.getId(), 7680));
        SkillMode mode = L1SkillMode.get().getSkill(4010);
        if (mode != null)
          try {
            mode.start(pc, null, null, 2400);
          } catch (Exception e) {
            _log.error(e.getLocalizedMessage(), e);
          }  
      } catch (Exception e2) {
        _log.error(e2.getLocalizedMessage(), e2);
      } 
    
    if (pc.hasSkillEffect(4010)) {
        try {
            L1QuestUser quest = WorldQuest.get().get(pc.get_showId());
            if (quest != null) {
              if (!quest.is_SpawnedDragon()) {
                checkDragonTimer2 timer = new checkDragonTimer2(npc.getMapId(), quest);
                timer.begin();
                quest.set_SpawnedDragon(true);
              } 
              L1Location loc = (new L1Location(32990, 32842, npc.getMapId())).randomLocation(5, false);
              L1Teleport.teleport(pc, loc.getX(), loc.getY(), npc.getMapId(), 4, true);
            } 
          } catch (Exception e) {
            _log.error(e.getLocalizedMessage(), e);
          } 
	}

  }
  
  public void action(L1PcInstance pc, L1NpcInstance npc, String cmd, long amount) {}
  
  private class checkDragonTimer2 extends TimerTask {
    private final int mapId;
    
    private final L1QuestUser quest;
    
    public checkDragonTimer2(int mapId, L1QuestUser quest) {
      this.mapId = mapId;
      this.quest = quest;
    }
    
    public void run() {
      cancel();  //停止计时器
      try {
        sendServerMessage(1657);
        Thread.sleep(2000L);
        sendServerMessage(1658);
        Thread.sleep(2000L);
        sendServerMessage(1659);
        Thread.sleep(2000L);
        sendServerMessage(1660);
        L1Location loc = (new L1Location(32958, 32835, this.mapId)).randomLocation(5, true);
        L1SpawnUtil.spawn(71026, loc, (new Random()).nextInt(8), this.quest.get_id());
      } catch (Exception exception) {
      
      } finally {
        Npc_DragonB2._timer.remove(Integer.valueOf(this.quest.get_id()));
      } 
    }
    
    private final void sendServerMessage(int msgid) {
      if (!this.quest.pcList().isEmpty()) {
        Iterator<L1PcInstance> iterator = this.quest.pcList().iterator();
        while (iterator.hasNext()) {
          L1PcInstance pc = iterator.next();
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(msgid));
        } 
      } 
    }
    
    public final void begin() {
      GeneralThreadPool.get().schedule(this, 30000L);
    }
  }
}
